#!/usr/bin/python

from sklearn import svm
from sklearn import preprocessing
import numpy
import sys
import pickle

execfile('/opt/python/includes/finance.py')

#selsvm=svm.SVC(kernel='poly',C=10000,verbose=True,degree=3)
#buysvm=svm.SVC(kernel='poly',C=10000,verbose=True,degree=3)
selsvm=svm.SVC(kernel='rbf',gamma=0.001,C=1000000,verbose=True)
buysvm=svm.SVC(kernel='rbf',gamma=0.001,C=1000000,verbose=True)

index=sys.argv[1]

X=[]

prices=getOHLCDict(index,'ohlcObjects')

#print "Comparing "+datefromsecs(prices[0]['date'])+" to " + datefromsecs(prices[1]['date'])+" and beyond...."

prices[0]['close_delta1'] = prices[0]['close']-prices[1]['close']
if prices[0]['close']<prices[1]['close']: prices[0]['close_up1'] = 0
else: prices[0]['close_up1'] = 1;

prices[0]['oc_delta1'] = prices[0]['close']-prices[0]['open']

prices[0]['close_delta2'] = prices[1]['close']-prices[2]['close']
if prices[1]['close']<prices[2]['close']: prices[0]['close_up2'] = 0
else: prices[0]['close_up2'] = 1;

prices[0]['oc_delta2'] = prices[1]['close']-prices[1]['open']

prices[0]['close_delta3'] = prices[2]['close']-prices[3]['close']
if prices[2]['close']<prices[3]['close']: prices[0]['close_up3'] = 0
else: prices[0]['close_up3'] = 1;

prices[0]['oc_delta3'] = prices[2]['close']-prices[2]['open']

prices[0]['close_delta4'] = prices[3]['close']-prices[4]['close']
if prices[3]['close']<prices[4]['close']: prices[0]['close_up4'] = 0
else: prices[0]['close_up4'] = 1;

prices[0]['oc_delta4'] = prices[3]['close']-prices[3]['open']

X.append(prices[0]['close_delta1'])
X.append(prices[0]['close_delta2'])
X.append(prices[0]['close_delta3'])
X.append(prices[0]['close_delta4'])
X.append(prices[0]['close_up1'])
X.append(prices[0]['close_up2'])
X.append(prices[0]['close_up3'])
X.append(prices[0]['close_up4'])
X.append(prices[0]['oc_delta1'])
X.append(prices[0]['oc_delta2'])
X.append(prices[0]['oc_delta3'])
X.append(prices[0]['oc_delta4'])

X=numpy.array(X)
X_norm=preprocessing.scale(X)
#print X_norm
if len(sys.argv)>2: selsvm=pickle.load(open("/opt/python/learning/"+sys.argv[2]+"-sel","rb"))
if len(sys.argv)>2: buysvm=pickle.load(open("/opt/python/learning/"+sys.argv[2]+"-buy","rb"))
print sys.argv[1]+" prediction: Sell "+str(selsvm.predict(X_norm))+", Buy "+str(buysvm.predict(X_norm))
